function MainAssistant() {
}

MainAssistant.prototype.setup = function() {

	// spinner
    this.spinner = this.controller.get('spin-scrim');
    this.spinnerAttrs = {
        	spinnerSize: 'large'
    	}
    this.spinnerModel = {
            spinning: false
    	}; 
    this.controller.setupWidget('feedSpinner', this.spinnerAttrs, this.spinnerModel);
	
	// create menu    
    this.appMenuAttr = {omitDefaultItems: true};
	this.appMenuModel = {
            visible: true,
            items: [
                { label: 'About...', command: 'do-about' }
            ]
        };
	this.controller.setupWidget(Mojo.Menu.appMenu, this.appMenuAttr, this.appMenuModel);
	
	// tagglebutton samba
	this.sambaToggleModel = {
	     value: false
	};
	this.controller.setupWidget("togglebuttonSamba",
         this.sambaToggleAttributes = {},
         this.sambaToggleModel);
	this.controller.listen("togglebuttonSamba", Mojo.Event.propertyChange,
			this.enableSamba.bindAsEventListener(this));
			
	// enable wifi
	this.enableWifiModel = {
            label : "Enable",
			buttonClass: 'affirmative',
            disabled: false
        };
	this.controller.setupWidget("buttonEnableWifi",
        {},
        this.enableWifiModel);
	this.controller.listen("buttonEnableWifi", Mojo.Event.tap,
			this.enableWifi.bindAsEventListener(this));
			
	// disable wifi
	this.disableWifiModel = {
            label : "Disable",
			buttonClass: 'negative',
            disabled: false
        };
	this.controller.setupWidget("buttonDisableWifi",
        {},
        this.disableWifiModel);
	this.controller.listen("buttonDisableWifi", Mojo.Event.tap,
			this.disableWifi.bindAsEventListener(this));
			
	// toggle button sleep
	this.sleepToggleModel = {
	     value: false
	};
	this.controller.setupWidget("togglebuttonSleep",
         {},
         this.sleepToggleModel);
	this.controller.listen("togglebuttonSleep", Mojo.Event.propertyChange,
			this.disableSleep.bindAsEventListener(this));
			
	this.setGUIStatus(true);
}

MainAssistant.prototype.enableWifi = function(event){
	this.controller.serviceRequest('palm://com.palm.wifi', {
		method: 'setstate',
		parameters: {
			'state': 'enabled'
		},
		onSuccess: this.enableWifiSuccessCallback.bind(this),
		onFailure: this.enableWifiFailureCallback.bind(this)
	});
}

MainAssistant.prototype.disableWifi = function(event){
	this.controller.serviceRequest('palm://com.palm.wifi', {
		method: 'setstate',
		parameters: {
			'state': 'disabled'
		},
		onSuccess: this.disableWifiSuccessCallback.bind(this),
		onFailure: this.disableWifiFailureCallback.bind(this)
	});
}

MainAssistant.prototype.enableWifiSuccessCallback = function() {
	//Mojo.Controller.errorDialog("Wifi enable success");
}

MainAssistant.prototype.enableWifiFailureCallback = function() {
	//Mojo.Controller.errorDialog("Wifi enable failure");
}

MainAssistant.prototype.disableWifiSuccessCallback = function() {
	//Mojo.Controller.errorDialog("Wifi disable success");
}

MainAssistant.prototype.disableWifiFailureCallback = function() {
	//Mojo.Controller.errorDialog("Wifi disable failure");
}

MainAssistant.prototype.disableSleep = function(event){
	if (event.value) {
		this.blockRequest = new Mojo.Service.Request("palm://com.palm.display/control", {
			method: "setProperty",
			parameters: {
				requestBlock: true, 
				client: "SambaPREnabler",
				subscribe: true
			},
			onSuccess: this.disableSleepSuccessCallback.bind(this),
			onFailure: this.disableSleepFailureCallback.bind(this)
		});
	} else {
		this.blockRequest.cancel();
		new Mojo.Service.Request("palm://com.palm.display/control", {
			method: "setProperty",
			parameters: {
				requestBlock: false,
				client: "SambaPREnabler"
			},
			onSuccess: this.enableSleepSuccessCallback.bind(this),
			onFailure: this.enableSleepFailureCallback.bind(this)
		});
	}
}

MainAssistant.prototype.disableSleepSuccessCallback = function() {
	//Mojo.Controller.errorDialog("Success disable display sleep.");
}

MainAssistant.prototype.disableSleepFailureCallback = function() {
	//Mojo.Controller.errorDialog("Failure disable display sleep.");
}

MainAssistant.prototype.enableSleepSuccessCallback = function() {
	//Mojo.Controller.errorDialog("Success enable display sleep.");
}

MainAssistant.prototype.enableSleepFailureCallback = function() {
	//Mojo.Controller.errorDialog("Failure enable display sleep.");
}

MainAssistant.prototype.enableSamba = function(event) {
	this.setGUIStatus(false);
	this.startSamba = event.value;
	this.controller.serviceRequest('palm://com.palm.app.tippix.sambaprenabler', {
			method: "enable",
			parameters: {
				enabled: event.value
			},
			onSuccess: this.enableSambaSuccessCallback.bind(this),
			onFailure: this.enableSambaFailureCallback.bind(this)
		});
}

MainAssistant.prototype.enableSambaSuccessCallback = function(result) {
	if (this.startSamba == true) {
		if ((result.result == "0") || (result.result == "1")) {
			Mojo.Controller.errorDialog("Samba didn't start successfully.");
		}
	}
	else {
		if ((result.result != "0") && (result.result != "1")) {
			Mojo.Controller.errorDialog("It seems Samba is still running.");
		}
	}
	this.setGUIStatus(true);
}


MainAssistant.prototype.enableSambaFailureCallback = function(result){
	Mojo.Controller.errorDialog("Failure calling Java service.");
	this.setGUIStatus(true);
}

MainAssistant.prototype.setGUIStatus = function(enabled) {
	if (enabled) {
		this.spinner.hide();
		this.spinnerModel.spinning = false;
	}
	else {
		this.spinner.show();
		this.spinnerModel.spinning = true;		
	}
	this.controller.modelChanged(this.spinnerModel);

	this.setWifiIP();
}

MainAssistant.prototype.setWifiIP = function() {
	this.controller.serviceRequest("palm://com.palm.connectionmanager", {
	    method: "getStatus",
	    parameters: {subscribe:true},
	    onSuccess: this.getStatusSuccessCallback.bind(this),
	    onFailure: this.getStatusFailureCallback.bind(this)
	});
}

MainAssistant.prototype.getStatusSuccessCallback = function(result) {
	if (result.wifi.state == "connected") {
		this.controller.get("divWlanIP").innerHTML = result.wifi.ipAddress;
	}
	else {
		this.controller.get("divWlanIP").innerHTML = "Disconnected";
	}
}

MainAssistant.prototype.getStatusFailureCallback = function(result) {
	this.controller.get("divWlanIP").innerHTML = "Error";
}

MainAssistant.prototype.activate = function(event) {
	this.controller.serviceRequest('palm://com.palm.app.tippix.sambaprenabler', {
			method: "isRunning",
			parameters: {},
			onSuccess: this.isRunningSambaSuccessCallback.bind(this),
			onFailure: this.isRunningSambaFailureCallback.bind(this)
		});
	this.setWifiIP();
}

MainAssistant.prototype.isRunningSambaSuccessCallback = function(result) {
	if ((result.result == "0") || (result.result == "1")) {
		this.sambaToggleModel.value = false;
	}
	else {
		this.sambaToggleModel.value = true;
	}
	this.controller.modelChanged(this.sambaToggleModel);
}


MainAssistant.prototype.isRunningSambaFailureCallback = function(result){
}

MainAssistant.prototype.deactivate = function(event) {
}

MainAssistant.prototype.cleanup = function(event) {
	this.enableSleep();
	Mojo.Event.stopListening('togglebuttonSamba', Mojo.Event.propertyChange, this.enableSamba);
	Mojo.Event.stopListening('togglebuttonSleep', Mojo.Event.propertyChange, this.disableSleep);
	Mojo.Event.stopListening('buttonEnableWifi', Mojo.Event.Tap, this.enableWifi);
	Mojo.Event.stopListening('buttonDisableWifi', Mojo.Event.Tap, this.disableWifi);
}

MainAssistant.prototype.handleCommand = function(event) {
	if (event.type == Mojo.Event.command) {
		this.cmd = event.command;

		switch(this.cmd) {
            
            case 'do-about':
                this.controller.showAlertDialog({
                    onChoose: function(value) {},
                    title: $L("SambaPREnabler " + Mojo.Controller.appInfo.version),
                    message: $L("Copyright 2009, Uwe Mannl"),
                    choices:[
                        {label:$L("OK"), value:""}
                    ]
                });
                break;

		} //switch
	} //if
}
